﻿using Simple.Common;
using Simple.Entity.Dto;
using Simple.Entity.Entity;
using Simple.Entity.Params;
using Simple.Entity.View;
using Simple.IRepository;
using Simple.IService;
using Simple.Tools;
using Simple.Tools.SnowFlake;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BaseMessage = Simple.Common.BaseMessage;

namespace Simple.Service
/// <summary>
/// 版 本 钢易智企2.0 V 2.0.0 钢易智企2.0
/// Copyright (c) 2023 杭州钢易科技有限公司
/// 创建人：曾东明
/// 日 期：2024
/// 描 述：
/// </summary>
{
	public class MenuService :BaseService,IMenuService
	{

		[Autowired]
		public IMenuRepository menuRepository { get; set; }

		[Autowired]

		public IRoleRepository roleRepository { get; set; }

        [Autowired]
        public IRoleMenuRepsitory roleMenuRepsitory { get; set; }

		[Autowired]

		public IUserMenuRepository userMenuRepository { get; set; }

        [Autowired]
        public ISimpleMapper mapper { get; set; }

        [Autowired]
		public ConnectionConfig connectionConfig { get; set; }

		[Autowired]

		public ISqlSugarClient _client { get; set; }
		public MenuService() { }

		public void TestTra()
		{
			SqlSugarClient client = (SqlSugarClient)_client;

			List<SysRoleEntity> roles=new List<SysRoleEntity>();
			bool result;
			for (int i = 0; i < 100; i++)
            {
				for (int j = 0; j < 100; j++)
				{
					SysRoleEntity role = new SysRoleEntity()
					{
						Id = Worker.GetId()
					};
					roles.Add(role);
				}
				result=roleRepository.CreateAsync(roles).Result;
				roles.Clear();

			}
		
			result = menuRepository.CreateAsync(new Entity.Entity.SysMenuEntity()
			{
				Id = Worker.GetId()
			}).Result;

			client.CommitTran();

		}

		/// <summary>
		///  添加菜单
		/// </summary>
		/// <returns></returns>
		[Transaction]		
		public async Task<BaseMessage> AddAsync(MenuDto menuDto)
		{
            if (await menuRepository.AnyAsync(m=>m.MenuType==menuDto.MenuType&& m.MenuName==menuDto.MenuName && m.IsDelete==false))
            {
				return Bad400("菜单名称重复!");
            }

			SysMenuEntity menu = new SysMenuEntity(true);

			if (await menuRepository.AnyAsync(m=>m.Id==menuDto.ParentId && m.IsDelete==false))
			{
				SysMenuEntity parentMenu = await menuRepository.FirstOrDefaultAsync(m => m.Id == menuDto.ParentId && m.IsDelete == false);

				if (parentMenu.IsChild==false)
				{
					await menuRepository.UpdateAsync(parentMenu);
				}
			}
			else
			{
				menuDto.ParentId = 0;
			}
			mapper.Map(menuDto,menu);

			var result=await menuRepository.CreateAsync(menu);

			if (result)
			{
				return Ok200("菜单添加成功!");
			}
			return Bad400("菜单添加失败!");
		}

		/// <summary>
		/// 更新菜单
		/// </summary>
		/// <param name="Id"></param>
		/// <param name="menuDto"></param>
		/// <returns></returns>
		public async Task<BaseMessage> UpdateAsync(long Id,MenuDto menuDto)
		{
			if (!await menuRepository.AnyAsync(m => m.Id==Id&& m.IsDelete == false ))
			{
				return Bad400("更新失败!");
			}

			if (await menuRepository.AnyAsync(m => m.Id!= Id && m.IsDelete == false&&  m.MenuType == menuDto.MenuType && m.MenuName == menuDto.MenuName))
			{
				return Bad400("更新失败,菜单名称重复!");
			}

			var menu = await menuRepository.FirstOrDefaultAsync(m => m.Id == Id && m.IsDelete == false);

			mapper.Map(menuDto, menu);

			var result = await menuRepository.UpdateAsync(menu);

			if (result)
			{
				return Ok200("更新成功!");
			}
			return Bad400("更新失败!");
		}

		/// <summary>
		/// 删除菜单
		/// </summary>
		/// <param name="Id"></param>
		/// <param name="menuDto"></param>
		/// <returns></returns>
		public async Task<BaseMessage> SoftDeleteAsync(long Id)
		{
			if (!await menuRepository.AnyAsync(m => m.Id == Id && m.IsDelete == false))
			{
				return Bad400("删除失败!");
			}

			if (await roleMenuRepsitory.AnyAsync(rm=>rm.MenuId==Id))
			{
				return Bad400("删除失败,已有角色绑定该菜单!");
			}

			if (await userMenuRepository.AnyAsync(rm => rm.MenuId == Id))
			{
				return Bad400("删除失败,已有用户绑定该菜单!");
			}

			var menu = await menuRepository.FirstOrDefaultAsync(m => m.Id == Id && m.IsDelete == false);

			menu.IsDelete = true;

			var result = await menuRepository.UpdateAsync(menu);

			if (result)
			{
				return Ok200("删除成功!");
			}
			return Bad400("失败!");
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public async Task<BaseMessage> GetPageAsync(MenuParameter Params)
		{

			var query=menuRepository.Query(q =>
			{
			
			});
			var data = await PageList<MenuView>.CreateAsync(query, Params);
			if (Params.IsTree)
			{
				return Ok200(data.ToList().ConvertTree<MenuView, string>());
			}
			return Ok200(data);

		}

	}
}
